package org.linys.action.station;


import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.linys.action.BaseAction;
import org.linys.model.station.Station;
import org.linys.model.system.Operator;
import org.linys.service.station.StationServ;
import org.linys.util.PageUtil;
import org.linys.util.ResponseMsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
@SuppressWarnings({  "unchecked" })
@Controller("StationAction")
@Scope("prototype")
public class StationAction extends BaseAction implements ModelDriven{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Station station= new Station();
	private ResponseMsgUtil responseMsgUtil;
	private PageUtil pageUtil;
	private String cityIDsStr;
	private StationServ stationServ; 
	
	/**
	 * 查看所有的网点
	 */
	public void view() {
		JSONObject object = new JSONObject();
		try{
			this.pageUtil.cal(stationServ.count(station), station.getRows(), station.getPage());
			List<Station> stations = stationServ.view(station, this.pageUtil.getStart(), this.pageUtil.getRows());
			
			JSONArray jsonArray = JSONArray.fromObject(stations);
			object.put("total", this.pageUtil.getTotal());
			object.put("rows", jsonArray);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			this.responseMsgUtil.doResponse(object.toString());
		}
	}
	
	
	/**
	 * 添加
	 */
	public void add() {
		JSONObject object = new JSONObject();
		try{
			Operator currentOperator=(Operator)request.getSession().getAttribute("operator_session");
			station.setHandlerID(currentOperator.getOperatorID_());
			station.setOperatorID(currentOperator.getOperatorID_());
			if (stationServ.save(station) > 0) {
				object.put("success", 1);
				object.put("stationID", station.getStationID());
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			this.responseMsgUtil.doResponse(object.toString());
		}
	}
	
	/**
	 * 修改网点
	 */
	public void update() {
		JSONObject object = new JSONObject();
		try{
			
			Operator currentOperator=(Operator)request.getSession().getAttribute("operator_session");
			station.setOperatorID(currentOperator.getOperatorID_());
			if (stationServ.update(station) > 0) {
				object.put("success", 1);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}finally{
			this.responseMsgUtil.doResponse(object.toString());
		}
	}
	
	/**
	 * 批量删除用户
	 */
	public void deleteByIDs() {
		String[] idsStr = station.getIds().split(",");
		for (String id : idsStr) {
			try {
				stationServ.deleteByID(new Integer(id));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		JSONObject object = new JSONObject();
		object.put("success", 1);
		this.responseMsgUtil.doResponse(object.toString());
	}

	/**
	 * 选择地域城市
	 */
	public void choose() {
		try{
			
			this.pageUtil.cal(stationServ.countChoose(station), station.getRows(), station.getPage());

			List<Station> citys = stationServ.viewChoose(station, this.pageUtil.getStart(), this.pageUtil.getRows());
			JSONArray jsonArray = JSONArray.fromObject(citys);
			JSONObject object = new JSONObject();
			object.put("total", this.pageUtil.getTotal());
			object.put("rows", jsonArray);
			this.responseMsgUtil.doResponse(object.toString());
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	public ResponseMsgUtil getResponseMsgUtil() {
		return responseMsgUtil;
	}
	@Autowired
	public void setResponseMsgUtil(ResponseMsgUtil responseMsgUtil) {
		this.responseMsgUtil = responseMsgUtil;
	}
	
	public PageUtil getPageUtil() {
		return pageUtil;
	}
	@Autowired
	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}
	public Object getModel() {
		return station;
	}
	public String getCityIDsStr() {
		return cityIDsStr;
	}
	public void setCityIDsStr(String cityIDsStr) {
		this.cityIDsStr = cityIDsStr;
	}
	public StationServ getStationServ() {
		return stationServ;
	}
	@Autowired
	public void setStationServ(StationServ stationServ) {
		this.stationServ = stationServ;
	}
	
}
